<?php
/**
 * 单图片上传统一接口类
 * Created by PhpStorm.
 * User: lixuemin
 * Date: 2019/7/1
 * Time: 9:15
 */

namespace app\flash\controller;

use think\Controller;
use think\Request;

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
//header('Access-Control-Allow-Methods: POST,GET','OPTIONS');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http动作
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
    exit;
}

class Upload extends Controller
{
    public function upload()//layer的上传图片接口，目前使用与封面
    {
        $typeArr = array("jpg", "png", "gif", "jpeg");//允许上传文件格式
        $filename = input('filename');
        if(!empty($filename)){
            $pathurlname = "upload/".$filename."/";
            if (!file_exists($pathurlname)){
                mkdir($pathurlname,777,true);
            }
            $pathurl = "upload/".$filename."/".date('Y-m-d',time())."/";
            if (!file_exists($pathurl)){
                mkdir($pathurl,777,true);
            }
        }else{
            $pathurl = "upload/other/".date('Y-m-d',time())."/";
            if (!file_exists($pathurl)){
                mkdir($pathurl,777,true);
            }
        }
        $path = $pathurl;//上传路径
        if (isset($_POST)) {
            if(empty($_FILES)){
                return json(msg(0, '', '您还未选择图片'));
            }
            $name = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $name_tmp = $_FILES['file']['tmp_name'];
            if (empty($name)) {
                return json(msg(0, '', '您还未选择图片'));
            }
            $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型

            if (!in_array($type, $typeArr)) {
                return json(msg(0, '', '上传失败！请上传jpg,png或gif类型的图片！'));
            }
            if ($size > (1000 * 1024)) {
                return json(msg(0, '', '上传失败！图片大小已超过1M！'));
            }
            $pic_name = time() . rand(10000, 99999) . "." . $type;//图片名称
            $pic_url = $path . $pic_name;//上传后图片路径+名称
            $okurl = $pathurl.$pic_name;
            if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
                $data=array("src"=>$okurl,"title"=>$pic_name);
                return json(msg(1,$data, '上传成功'));
            } else {
                return json(msg(0, '', '上传有误，清检查服务器配置！'));
            }
        }
    }

    public function upload2()//layer的上传图片接口，目前使用与封面
    {
        $typeArr = array("jpg", "png", "jpeg");//允许上传文件格式
        $filename = input('filename');
        if(!empty($filename)){
            $pathurlname = "upload/".$filename."/";
            if (!file_exists($pathurlname)){
                mkdir($pathurlname,0777,true);
            }
            $pathurl = "upload/".$filename."/".date('Y-m-d',time())."/";
            if (!file_exists($pathurl)){
                mkdir($pathurl,0777,true);
            }
        }else{
            $pathurl = "upload/other/".date('Y-m-d',time())."/";
            if (!file_exists($pathurl)){
                mkdir($pathurl,0777,true);
            }
        }
        $path = $pathurl;//上传路径
        if (isset($_POST)) {
            if(empty($_FILES)){
                return json_encode(array('status'=>'0',"msg"=>"上您还未选择图片"));
            }
            $name = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $name_tmp = $_FILES['file']['tmp_name'];
            if (empty($name)) {
                return json_encode(array('status'=>'0',"msg"=>"上您还未选择图片"));
            }
            $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型

            if (!in_array($type, $typeArr)) {
                return json_encode(array('status'=>'0',"msg"=>"上上传失败！请上传jpg,png或gif类型的图片！"));
            }
            if ($size > (1000 * 1024)) {
                return json(msg(0, '', '上传失败！图片大小已超过1M！'));
            }
            $pic_name = time() . rand(10000, 99999) . "." . $type;//图片名称
            $pic_url = $path . $pic_name;//上传后图片路径+名称
            $okurl = $pathurl.$pic_name;
            if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
                $data=array("src"=>$okurl,"title"=>$pic_name);
                return json_encode(array('status'=>'1','data'=>$data,"msg"=>"上传成功"));
            } else {
                return json_encode(array('status'=>'0',"msg"=>"上传有误，清检查服务器配置！"));
            }
        }
    }

    /*public function more_upload(){
        $filename = input('filename');
        if(!empty($filename)){
            $pathurlname = "upload/".$filename."/";
            if (!file_exists($pathurlname)){
                mkdir($pathurlname,777,true);
            }
            $pathurl = "upload/".$filename."/".date('Y-m-d',time())."/";
            if (!file_exists($pathurl)){
                mkdir($pathurl,777,true);
            }
        }else{
            $pathurl = "upload/other/".date('Y-m-d',time())."/";
            if (!file_exists($pathurl)){
                mkdir($pathurl,777,true);
            }
        }
        $path = $pathurl;//上传路径
        $arr = array();
        foreach($_FILES as $file){
            $name = $file['name'];
            $name_tmp = $file['tmp_name'];
            $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型
            $pic_name = time() . rand(10000, 99999) . "." . $type;//图片名称
            $pic_url = $path . $pic_name;//上传后图片路径+名称
            $okurl = $pathurl.$pic_name;
            if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
                $data=array("src"=>$okurl,"title"=>$pic_name);
            } else {
                return json(msg(0, '', '上传有误，请检查服务器配置！'));
            }
            array_push($arr,$data);
        }
        return json(msg(1,$arr, '上传成功'));
    }*/

    public function more_upload(Request $request){
        $files = $request->file("picture");
        if(count($files)==0){
            return json(['code' => 0, 'msg' => '您为上传图片']);
        }
        $filename = input('filename');
        $url = '';
        if(!empty($filename)){
            $pathurlname = "upload/".$filename."/";
            if (!file_exists($pathurlname)){
                mkdir($pathurlname,777,true);
            }
        }else{
            $pathurlname = "upload/other/";
        }
        if (is_array($files)) {
            foreach ($files as $vo) {
                $info = $vo->move($pathurlname);
                if ($info) {
                    $url  .= $pathurlname . date('Ymd') . '/' . $info->getFilename().',';
                } else {
                    return json(['code' => 0, 'msg' => $vo->getError()]);
                }
            }
        } else {
            $info = $files->move($pathurlname);
            if ($info) {
                $url .= $pathurlname . date('Ymd') . '/' . $info->getFilename();
            } else {
                return json(['code' => 0, 'msg' => $files->getError()]);
            }
        }
        $url = rtrim($url, ',');
        return json(['code' => 1, 'msg' => '上传成功！', 'url' => $url]);
    }
}